package com.xthink.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xthink.common.result.AjaxResult;
import com.xthink.common.pojo.order.Order;
import com.xthink.common.vo.cards.DetailsOrderVo;
import com.xthink.common.vo.order.OrderListVo;
import com.xthink.web.mapper.OrderMapper;
import com.xthink.web.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    //分页及模糊查询
    @Override
    public AjaxResult orderLikeList(OrderListVo orderListVo) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(
                orderListVo.getCreatTimeStart() != null && orderListVo.getCreatTimeEnd() != null
                , Order::getCreateTime
                , orderListVo.getCreatTimeStart()
                , orderListVo.getCreatTimeEnd());

        wrapper.between(orderListVo.getUpdateTimeStart() != null && orderListVo.getUpdateTimeEnd() != null
                , Order::getUpdateTime
                , orderListVo.getUpdateTimeStart()
                , orderListVo.getUpdateTimeEnd());

        wrapper.like(!StringUtils.isNotBlank(orderListVo.getIccId()), Order::getIccId, orderListVo.getIccId());
        wrapper.eq(orderListVo.getStatus() != null, Order::getStatus, orderListVo.getStatus());
        wrapper.eq(orderListVo.getTransactionType() != null, Order::getTransactionType, orderListVo.getTransactionType());

        Page<Order> orderPage = new Page<>(orderListVo.getPage(), orderListVo.getSize());
        Page<Order> page = orderMapper.selectPage(orderPage, wrapper);
        List<Order> orders = page.getRecords();
        if (orders.size() == 0) {
            return AjaxResult.error("未找到此订单");
        }
        return AjaxResult.success();
    }

    //退款
    @Override
    public AjaxResult drawBack(Integer[] ids) {
        for (Integer id : ids) {
            Order order = orderMapper.selectById(id);
            Integer status = order.getStatus();
            if (status==2){
                return AjaxResult.error("该订单已为退款状态，不能进行退款");
            }
            if (status==3){
                return AjaxResult.error("该订单为已结束，不能进行退款");
            }
            LambdaUpdateWrapper<Order> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Order::getId,id).set(Order::getStatus,2);
            int update = orderMapper.update(null, wrapper);
            if (update==0){
                return AjaxResult.error("退款失败");
            }
        }
        return AjaxResult.success();
    }

    //卡片详情中的订单列表
    @Override
    public AjaxResult detailsOrder(DetailsOrderVo detailsOrderVo) {

        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        Page<Order> page = new Page<>(detailsOrderVo.getPage(), detailsOrderVo.getSize());

        wrapper.eq(Order::getCardId,detailsOrderVo.getCardId());
        Page<Order> orderPage = orderMapper.selectPage(page, wrapper);
        List<Order> records = orderPage.getRecords();
        if (records.size()==0){
            return AjaxResult.error("该卡片暂未有订单数据");
        }
        return AjaxResult.success();
    }
}
